<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

////////////////////////////////////////////////////////////////
//
//	ROOT OBJECT
//
//	OBJECT MODEL
//		static
//		const
//		public, protected, private
//		abstract
//		class, interface
//		extends, implements
//		self, parent
//		::
//		::$		-> access static
//
////////////////////////////////////////////////////////////////

class CoreObject {
	var $_instances = array();
	
	//constructor
	function __construct() {
		
	}
	
	function __destruct() {
		
	}
	
	function __set($name, $value) {
		if( isset($this->$name) ) {
			$this->$name = $value;
		}
	}
	
	function __get($name) {
		if( isset($this->$name) ) {
			return $this->$name;
		}
		return NULL;
	}
	
	function __autoload($class_name) {
		
	}
	
	//
	function error($msg, $stop = 1) {
		echo $msg;
		exit();
	}
	
	//methods
	//LOAD
	function load($file, $type = 'class') {
		if( $type == 'class' && $file != '' && file_exists(CONFIG_PATH . CONFIG_DIR_CLASSES . $file) ) {
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $file);
		} elseif( $type == 'function' && $file != '' && file_exists(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . $file) ) {
			include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . $file);
		}
	}
	
	function loadclass($classfile) {
		if( file_exists(CONFIG_PATH . CONFIG_DIR_CLASSES . $classfile) ) {
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $classfile);
		}
	}
	
	//OBJECT
	function &getObj($class, $file = '') {
		if( $file != '' && file_exists(CONFIG_PATH . CONFIG_DIR_CLASSES . $file) ) {
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $file);
		}
		if( class_exists($class) ) {
			$obj = new $class();
			$obj->init();
			
			return $obj;
		}
		
		return NULL;
	}
	
	//INSTANCE
	function instance($class, $singleton = false, $file = '') {
		if( $singleton == false && isset($this->_instances[$class]) ) {
			return $this->_instances[$class];
		}
		$this->loadclass($file);
		
		if( class_exists($class) ) {
			$obj = new $class();
			$obj->init();
			
			if( $singleton == false ) {
				$this->_instances[$class] = $obj;
			}
			
			return $obj;
		} else {
			$this->error('Class ' . $classname);
		}
	}
	
	function instanceCall($class, $method = '', $params = '', $type = '_array_', $singleton = false, $file = '') {
		$obj = $this->instance($class, $singleton, $file);
		
		if( is_object($obj) ) {
			if( $method != '' && method_exists($obj, $method) ) {
				$this->call($obj, $method, $params, $type);
			}
		}
		
		return $obj;
	}
	
	//call
	//	obj, class name or class object
	function call($obj, $method, $params = '', $type = '_array_') {
		if( empty($obj) ) {
			return -1;
		}
		if( !is_object($obj) ) {
			$obj = $this->getObj($obj);
			if( $obj == NULL ) {
				return -2;
			}
		}
		if( method_exists($obj, $method) ) {
			if( $type != '_array_' ) {
				$paramsb = array();
				$paramsb[] = $params;
				$paramsb[] = $type;
				$args = func_get_args();
				for($i=4, $n=sizeof($args); $i<$n; $i++) {
					$paramsb[] = $args[$i];
				}
				$params = $paramsb;
			} elseif( !is_array($params) ) {
				$paramsb = array();
				$paramsb[] = $params;
				$params = $paramsb;
			}
			//return call_user_func(array($obj, $method), $params);
			if( sizeof($params) == 0 ) {
				$data = $obj->$method();
			} elseif( sizeof($params) == 1 ) {
				$data = $obj->$method($params['0']);
			} elseif( sizeof($params) == 2 ) {
				$data = $obj->$method($params['0'], $params['1']);
			} elseif( sizeof($params) == 3 ) {
				$data = $obj->$method($params['0'], $params['1'], $params['2']);
			} elseif( sizeof($params) == 4 ) {
				$data = $obj->$method($params['0'], $params['1'], $params['2'], $params['3']);
			} elseif( sizeof($params) == 5 ) {
				$data = $obj->$method($params['0'], $params['1'], $params['2'], $params['3'], $params['4']);
			} elseif( sizeof($params) == 6 ) {
				$data = $obj->$method($params['0'], $params['1'], $params['2'], $params['3'], $params['4'], $params['5']);
			} else {
				$data = $obj->$method($params['0'], $params['1'], $params['2'], $params['3'], $params['4'], $params['5'], $params['6']);
			}
			
			return $data;
		}
		
		return -3;
	}
	
	function callb($obj, $method, $params = '', $type = '_array_') {
		
	}
	
	//bind array data to object attribute
	function bindArray($data_array) {
		if( is_array($data_array) ) {
			reset($data_array);
			//foreach($data_array as $k => $v) {
			//	$this->$k = func_db_input($v);
			//}
			while(list($k, $v) = each($data_array)) {
				$this->$k = func_db_input($v);
			}
		}
	}
}
//
?>